#include "GUI.h"
#include "std_lib_facilities.h"
#include "Graph.h"
#include <FL/Fl_Multiline_Output.H>

using namespace Graph_lib;
string name2 = "scores.txt";

class Position : public Button {
	public:
	Position(Point xy, int w, int h, const string& s ,Callback cb)
	:Button(xy,50,50,s,cb)
	{
	}
	Address get_fltk_button_address()
	{
		return pw;
	}
	
	Point get_point() 
	{
		return loc;
	}	
};


struct Checkerboard : Window {
    Checkerboard(Point xy, int w, int h, const string& title )
	: Window(xy,w,h,title),
	quit_button(Point(230,415), 169,39, "", cb_quit),	//define buttons
	start_button(Point(230,293),169,39, "", cb_start),
	score_button(Point(230,356),169,39, "", cb_score),
	menu_button(Point(233,435) ,173,28, "", cb_menu),
	next_button(Point(369,436) ,157, 25,"", cb_next),
	game_button(Point(487,410) ,111, 30,"", cb_game),
	back_button(Point(103,436) ,157, 25,"", cb_back),
	winner_button(Point(194,420),219,29,"" , cb_final),
	board_image(Point(0,0),"board.jpg"),
	main_image(Point(0,0),"main_menu.jpg"),				//and images
	score_image(Point(0,0),"scores.jpg"),
	inst_image(Point(0,0),"instructions.jpg"),
	namein_image(Point(0,0),"name.gif"),
	winner_image(Point(0,0),"winner.jpg"),
	hi_score(Point(130,100),""),
	namein(Point(220,259),188,38,"")
	

	{	
    attach(quit_button);
	attach(start_button);
	attach(score_button);			//attach base UI
	attach(main_image);

	}
	Image board_image;
	Image main_image;
	Image score_image;
	Image namein_image;
	Image winner_image;
	Image inst_image; 		//declare images
	Button back_button;				//and buttons
	Button next_button;
	Button game_button;
	Button quit_button;
	Button winner_button;
	Button start_button;
	Button score_button;
	Button menu_button;
	In_box namein;
	Text hi_score;
	  
	 void draw_position_buttons(){ //Draws a board of buttons, only on the dark spaces
	 for(int i =0; i<8; ++i){
		for(int j = 0; j<8;++j){
			if(((i+j)%2)){
			Position *b = new Position(Point((i*50)+120,(j*50)+40),50,50,"", cb_posi);
			pr.push_back(b);
			pieces.push_back(new Image(Point((i*50)+120,(j*50)+40), "UT_Checker.gif")); //make a vector of images
			aggies.push_back(new Image(Point((i*50)+120,(j*50)+40), "A_M_Checker.gif")); 
			attach(pr[pr.size()-1]);
			ar.push_back(b->get_fltk_button_address());
			}
			}
		
		}
	}
	
private:

		Vector_ref<Position> pr;
		Vector<Address> ar;
		Vector_ref<Image> pieces;
		Vector_ref<Image> aggies;
		ifstream ifs;
		

		//callbacks
		static void cb_quit(Address, Address addr) 
		{
		static_cast<Checkerboard*>(addr)->quit();
		}
		
		static void cb_start(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->start();
		}
		
		static void cb_score(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->score();
		}

		static void cb_menu(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->menu();
		}
			
		static void cb_back(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->back();
		}
		
		static void cb_next(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->next();
		}
		
		static void cb_game(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->game();
		}
		
		static void cb_final(Address, Address addr)
		{
		static_cast<Checkerboard*>(addr)->final();
		}
		
		static void cb_posi(Address w,Address pw)
	  	{
		reference_to<Checkerboard>(pw).pushed(w);
		}
		void winner();
		int check_conflict(int pos);
		void aggie_ai();
		void delete_ze_aggies(int c);
		void delete_ze_image();
		void check_move();
		void save_name();
		void pushed(Address b1); 	//~~draws pieces~~
									//Check Main.cpp
		
									//so many callbacks...
		void quit(){ hide();} 
									//~~Start Menu~~
		void start() { 				//hides the base menu
		start_button.hide();		//pulls up instructions
		score_button.hide();
		quit_button.hide();
		attach(back_button);
		attach(next_button);
		detach(main_image);
		attach(inst_image);
		
		}
									//~~High Score Screen~~	
		void score(){ 				//hides the base menu
		score_button.hide(); 		//pulls up scores
		start_button.hide();
		quit_button.hide();
		detach(main_image);
		attach(score_image);
		attach(menu_button);
		stringstream buffer;
		ifs.open(name2.c_str(), ios::in);
		buffer << ifs.rdbuf();
		string z(buffer.str());
		hi_score.set_label(z.c_str());
		hi_score.set_font_size(25);
		attach(hi_score);
		ifs.close();
		} 
									//~~Main Menu~~
		void menu(){ 				//returns to the base menu
		detach(score_image); 		//from score screen
		detach(hi_score);
		detach(menu_button); 		//hides score screen
		attach(score_button);
		attach(start_button);
		attach(quit_button);
		attach(main_image);
		}
									//~~Back to Main Screen~~
		void back(){ 				//returns to the base menu
		detach(inst_image); 		//from instructions
		detach(back_button); 		//hides instructions
		detach(next_button);
		attach(score_button);
		attach(start_button);
		attach(quit_button);
		attach(main_image);
		}
									//~~Loads Game~~
		void next(){ 				//Detach Instructions
		detach(back_button); 		//Proceed to games
		detach(next_button);
		detach(inst_image);
		attach(namein_image);
		attach(game_button);
		attach(namein);
		}
		void game(){
		detach(namein);
		save_name();
		detach(namein_image);
		detach(game_button);
		draw_position_buttons();
		attach(board_image);
		attach(aggies[4]);
		attach(aggies[12]);
		attach(aggies[20]);
		attach(aggies[28]);
		}
		void final(){
		detach(winner_image);
		detach(winner_button);
		attach(menu_button);
		attach(score_image);
		stringstream buffer;
		ifs.open(name2.c_str(), ios::in);
		buffer << ifs.rdbuf();
		string z(buffer.str());
		hi_score.set_label(z.c_str());
		hi_score.set_font_size(25);
		attach(hi_score);
		ifs.close();
		}
		
};

